package com.oyc.demo.controller;

import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oyc.demo.domain.Enter;
import com.oyc.demo.domain.Passrate;
import com.oyc.demo.service.CablebasicService;

import io.swagger.annotations.ApiOperation;
/**
 * 统计分析
 *
 * @author 杜学森
 * @since 2021-01-20 18:51:53
 */
@RestController
@RequestMapping("/icon")
public class IconController {
	
	/**
     * 服务对象
     */
    @Autowired
    private CablebasicService cablebasicService;
    
    SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");  
	
	@RequestMapping(value="/selectenter",method= RequestMethod.POST)
    @ApiOperation("统计分析")
    public JSONObject selectenter(HttpServletRequest requetst) {
		 	PageHelper.startPage(Integer.parseInt(requetst.getParameter("page")), Integer.parseInt(requetst.getParameter("rows")));
        	//省公司数据录入情况
        	List<Enter> enter=cablebasicService.enter();
        	
        	 PageInfo<Enter> pageInfo = new PageInfo<Enter>(enter);

             JSONObject oJson = new JSONObject();
             oJson.put("records", pageInfo.getTotal());
             oJson.put("total", pageInfo.getPages());
             oJson.put("page", pageInfo.getPageNum());
             
             List<Enter> list=pageInfo.getList();
             
             if(list!=null&&list.size()>0){
                 JSONArray array=new JSONArray();
                 for (int i = 0; i < list.size(); i++) {
                	 Enter map=list.get(i);
                     JSONObject obj=new JSONObject();
                     obj.put("id", map.getProvincecompanyname());
                     
                     JSONArray innerarray=new JSONArray();
                     innerarray.add(map.getProvincecompanyname());
                     innerarray.add(map.getTotal());
                     innerarray.add(map.getOk());
                     innerarray.add(map.getNo());
                     innerarray.add(formatterDecimal(map.getRate()));
                     obj.put("cell", innerarray);
                     array.add(obj);
                 }
                 oJson.put("rows", array);
             }
             
            return oJson;
    }
	
	@RequestMapping(value="/selectpassrate",method= RequestMethod.POST)
    @ApiOperation("统计分析")
    public JSONObject selectpassrate(HttpServletRequest requetst) {
		 	PageHelper.startPage(Integer.parseInt(requetst.getParameter("page")), Integer.parseInt(requetst.getParameter("rows")));
        	//省公司合格率
        	List<Passrate> passrate=cablebasicService.passrate();
        	
        	 PageInfo<Passrate> pageInfo = new PageInfo<Passrate>(passrate);

             JSONObject oJson = new JSONObject();
             oJson.put("records", pageInfo.getTotal());
             oJson.put("total", pageInfo.getPages());
             oJson.put("page", pageInfo.getPageNum());
             
             List<Passrate> list=pageInfo.getList();
             
             if(list!=null&&list.size()>0){
                 JSONArray array=new JSONArray();
                 for (int i = 0; i < list.size(); i++) {
                	 Passrate map=list.get(i);
                     JSONObject obj=new JSONObject();
                     obj.put("id", map.getProvincecompanyname());
                     
                     JSONArray innerarray=new JSONArray();
                     innerarray.add(map.getProvincecompanyname());
                     innerarray.add(formatterDecimal(map.getOk()));
                     innerarray.add(formatterDecimal(map.getAllno()));
                     innerarray.add(formatterDecimal(map.getAmongno()));
                     innerarray.add(formatterDecimal(map.getError()));
                     obj.put("cell", innerarray);
                     array.add(obj);
                 }
                 oJson.put("rows", array);
             }
             
            return oJson;
    }
	
	@RequestMapping(value="/upexportexcel",method= RequestMethod.GET)
    @ApiOperation("上表格导出")
    public void upexportexcel(HttpServletRequest requetst,HttpServletResponse response) {
        try {
        	//省公司合格率
        	List<Enter> enter=cablebasicService.enter();
        	
        	for (int i = 0; i < enter.size(); i++) {
        		Enter bean=enter.get(i);
        		bean.setRate(formatterDecimal(bean.getRate()));
        		enter.set(i, bean);
			}
        	
        	response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("成盘电缆检测结果省公司数据录入情况"+sdf.format(new Date()),"UTF-8") + ".xlsx");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            
            ServletOutputStream outputStream = response.getOutputStream();
            try {
            	 EasyExcel.write(outputStream, Enter.class).sheet("成盘电缆检测结果省公司数据录入情况").doWrite(enter);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                outputStream.close();
            }
        }catch (Exception e){
        	e.printStackTrace();
        }

    }
	
	@RequestMapping(value="/downexportexcel",method= RequestMethod.GET)
    @ApiOperation("下表格导出")
    public void downexportexcel(HttpServletRequest requetst,HttpServletResponse response) {
        try {
        	//省公司合格率
        	List<Passrate> passrate=cablebasicService.passrate();
        	
        	for (int i = 0; i < passrate.size(); i++) {
        		Passrate bean=passrate.get(i);
        		bean.setOk(formatterDecimal(bean.getOk()));
        		bean.setAllno(formatterDecimal(bean.getAllno()));
        		bean.setAmongno(formatterDecimal(bean.getAmongno()));
        		bean.setError(formatterDecimal(bean.getError()));
        		passrate.set(i, bean);
			}
        	
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("成盘电缆检测结果省公司合格率"+sdf.format(new Date()),"UTF-8") + ".xlsx");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            
            ServletOutputStream outputStream = response.getOutputStream();
            try {
            	 EasyExcel.write(outputStream, Passrate.class).sheet("成盘电缆检测结果省公司合格率").doWrite(passrate);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                outputStream.close();
            }
        }catch (Exception e){
        	e.printStackTrace();
        }

    }
	
	private String formatterDecimal(String dbField)

	{
		  if(dbField==null||dbField.trim().length()==0){
			  return dbField;
		  }

	       //将字符串强制转换成double类型

	      double number = Double.parseDouble(dbField);

          DecimalFormat df = new DecimalFormat("0.00%");
          String r = df.format(number);
          return r;

	}
	
	
}
